package com.hibiscusking.org.service.impl;


import com.hibiscusking.basic.service.impl.BaseServiceImpl;
import com.hibiscusking.org.domain.Employee;
import com.hibiscusking.org.domain.Shop;
import com.hibiscusking.org.mapper.EmployeeMapper;
import com.hibiscusking.org.mapper.ShopMapper;
import com.hibiscusking.org.service.EmployeeService;
import com.hibiscusking.user.domain.Logininfo;
import com.hibiscusking.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService  {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Override
    public void add(Employee employee) {
        // 把emp的值赋值给loginfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        // 添加loginfo
        logininfoMapper.add(logininfo);
        // emp获取loginfo的自增id
        employee.setLogininfoId(logininfo.getId());

        employeeMapper.add(employee);
    }

    @Override
    public void update(Employee employee) {
        // 查询修改的emp的loginfoId
        Employee emp = employeeMapper.findById(employee.getId());
        employee.setLogininfoId(emp.getLogininfoId());
        // 把emp的值赋值给loginfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setId(employee.getLogininfoId());
        logininfoMapper.update(logininfo);
        employeeMapper.update(employee);
    }

    @Override
    public void dele(Long id) {
        // 查询修改的emp的loginfoId
        Employee emp = employeeMapper.findById(id);
        Shop shop = new Shop();
        shop.setAdminId(emp.getId());
        shopMapper.updateShopByAdminId(shop);
        logininfoMapper.dele(emp.getLogininfoId());
        employeeMapper.dele(id);
    }

    @Override
    public Employee findByLoginId(Long id) {
        return employeeMapper.findByLoginId(id);
    }
}
